<?php

namespace app\controller;

use app\model\Activity;
use app\model\Catering;
use app\model\Hotel;
use app\model\Material;
use app\model\SkiingPark;
use app\model\User;
use think\Request;

class Admin extends Auth
{

    public function index()
    {
        return 'home';
    }

    public function users()
    {
        $list = User::where('status', '>', -1)->select();
        return view('users', ['list' => $list]);
    }

    public function skiingparks()
    {
        $list = SkiingPark::with('activitys,materials,hotels,caterings')->select();
        return view('skiingparks', ['list' => $list]);
    }

    public function upUser(Request $request)
    {
        $id   = $request->id;
        $user = User::find($id);
        return view('upuser', ['model' => $user]);
    }

    public function updateUser(Request $request)
    {
        $id   = $request->id;
        $user = User::find($id);
        switch ($request->action) {
            case 'ban':
                $user->status = 0;
                break;
            case 'rec':
                $user->status = 1;
                break;
            case 'del':
                $user->status = -1;
                break;
        }
        $user->save();
        $this->success('action success');
    }

    public function addSp()
    {
        return view('sp');
    }

    public function upSp(Request $request)
    {
        $id    = $request->id;
        $model = SkiingPark::find($id);
        return view('upsp', ['model' => $model]);
    }

    public function postSp(Request $request)
    {
        $model    = new SkiingPark;
        $data     = $request->post();
        $isUpdate = isset($data['id']) ? true : false;
        $model->isUpdate($isUpdate)->save($data);
        $this->redirect('admin/skiingparks');
    }

    public function postUser(Request $request){
        $model    = new User;
        $data     = $request->post();
        $isUpdate = isset($data['id']) ? true : false;
        $model->isUpdate($isUpdate)->save($data);
        $this->redirect('admin/users');
    }

    public function addChild(Request $request)
    {
        $sid  = $request->id;
        $sp   = SkiingPark::find($sid);
        $view = $request->action;
        return view($view, ['sid' => $sid, 'name' => $sp->name]);
    }

    public function upChild(Request $request)
    {
        $id   = $request->id;
        $type = $request->type;
        $view = 'up' . $type;
        switch ($type) {
            case 'catering':
                $model = new Catering;
                break;
            case 'activity':
                $model = new Activity;
                break;
            case 'material':
                $model = new Material;
                break;
            case 'hotel':
                $model = new Hotel;
                break;
            default:
                $this->error('param error');
        }
        $model = $model->find($id);
        return view($view, ['model' => $model]);
    }

    public function postChild(Request $request)
    {
        $type = $request->type;
        switch ($type) {
            case 'catering':
                $model = new Catering;
                break;
            case 'activity':
                $model = new Activity;
                break;
            case 'material':
                $model = new Material;
                break;
            case 'hotel':
                $model = new Hotel;
                break;
            default:
                $this->error('param error');
        }
        $data     = $request->post();
        $isUpdate = isset($data['id']) ? true : false;
        $model->isUpdate($isUpdate)->save($data);
        $this->redirect('admin/skiingparks');
    }
}
